package makai.image;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;

/**
 * A MakaiImage is an extension of a BufferedImage which allows for custom Image 
 * functions to be written directly into the class, such as blending or shifts.
 * 
 * @author Krechevskoy
 * @version 1.0
 */
public class MakaiImage extends BufferedImage implements Cloneable {

    public MakaiImage(int width, int height) {
        this(width, height, TYPE_INT_ARGB);
    }
    
    public MakaiImage(int width, int height, int imageType) {
        super(width, height, imageType);
    }

    public MakaiImage(int width, int height, int imageType, IndexColorModel colorModel) {
        super(width, height, imageType, colorModel);
    }
    
    /**
     * @return A Graphics2D object used to draw to this image
     */
    public Graphics2D getGraphics2D(){
        return (Graphics2D)getGraphics();
    }
    
    /**
     * @param width The width of the output image
     * @param height The height of the output image
     * 
     * @return A resized copy of this image
     */
    public MakaiImage getResized(int width, int height){
        return getResized(width, height, false);
    }
    
    /**
     * NOTE: If proportions are constrained, the given dimensions
     * will automatically be adjusted to the nearest fit
     * 
     * @param width The width of the output image
     * @param height The height of the output image
     * @param proportional If the dimensions should be resized proportionally
     * 
     * @return A resized copy of this image
     */
    public MakaiImage getResized(int width, int height, boolean proportional){
        
        if( proportional ){
            int deltaW, deltaH, fixedW, fixedH;
            double imageW, imageH, factor;
            
            imageW = getWidth();
            imageH = getHeight();
            
            deltaW = (int)imageW - width;
            deltaH = (int)imageH - height;
            
            if(deltaW > deltaH){
                factor = width/imageW;
            } else {
                factor = height/imageH;
            }
            
            fixedW = (int)Math.floor(imageW*factor);
            fixedH = (int)Math.floor(imageH*factor);
            
            return getResized(fixedW, fixedH, false);
        } else {
            MakaiImage out = new MakaiImage(width, height, getType());
            
            Graphics2D g2 = (Graphics2D)out.getGraphics();
            g2.setRenderingHint(
                    RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(this,0,0,width,height,null);
            g2.dispose();
            
            return out;
        }
    }

    @Override
    protected MakaiImage clone() {
        
        MakaiImage out = new MakaiImage(getWidth(), getHeight(), getType());
        out.setRGB(0, 0, getWidth(), getHeight(), getRGB(0, 0, getWidth(), getHeight(), null, 0, getWidth()), 0, getWidth());
        
        return out;
    }
    
}
